package com.tengju.data.infrastructure.repository.clickhouse.crm;

import com.tengju.data.domain.model.clickhouse.BrandAnalysisM;
import com.tengju.data.domain.model.clickhouse.DwdCrmDetailMRepository;
import com.tengju.data.domain.shared.Month;
import com.tengju.data.infrastructure.persistence.mybatis.dataobject.BrandAnalysisMVO;
import com.tengju.data.infrastructure.persistence.mybatis.mapper.clickhouse.crm.DwdCrmDetailMDOMapper;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.tengju.data.infrastructure.repository.clickhouse.convert.BrandAnalysisConvert;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

@Repository
public class DwdCrmDetailMRepositoryImpl implements DwdCrmDetailMRepository {

    private final DwdCrmDetailMDOMapper dwdCrmDetailMDOMapper;

    public DwdCrmDetailMRepositoryImpl(DwdCrmDetailMDOMapper dwdCrmDetailMDOMapper) {
        this.dwdCrmDetailMDOMapper = dwdCrmDetailMDOMapper;
    }

    @Override
    public BigDecimal getSumGMV(Long clubId, List<Integer> months) {
        return dwdCrmDetailMDOMapper.selectSumGMV(clubId, months);
    }

    @Override
    public List<BrandAnalysisM> listBrand(Month month, Long clubId) {
        List<BrandAnalysisMVO> brandAnalysisMVOList = dwdCrmDetailMDOMapper.listBrand(clubId, month.getMonthInt());
        if (CollectionUtils.isEmpty(brandAnalysisMVOList)) {
            return Collections.emptyList();
        }

        return brandAnalysisMVOList.stream()
                .map(BrandAnalysisConvert::toModel)
                .collect(Collectors.toList());
    }
}
